import { getToDoList, addToDoList, operate,updateType } from '../service';
import moment from 'moment';
const creator = (type, payload) => ({ type, payload });

const pieDataHandle=(arr)=>{
  const res=[]
  let hashSet=new Set();
  arr.forEach(item=>hashSet.add(item.type))
  for(const key of hashSet.keys()){
    let value=0;
    for(let i=0;i<arr.length;i++){
      if(key===arr[i].type){
        value++
      }
    }
    res.push({name:key,value})
  }
  return res
}
export default {
  namespace: 'plan',
  state: {
    toDoList: [],
    achievementList: [],
    valueArr: [],
    coin: [],
    xData: [], //x轴数据
    serName1: '', //成就值
    serName2: '', //成就币
    totalValue:0,//总成就值
    totalCoin:0,//总成就币
    pieData:[]
  },
  effects: {
    //获取列表
    *getToDoList({ payload }, { call, put }) {
      const res = yield call(getToDoList, payload);
      if (res.httpStatus === 200) {
        let toDoList= res.data.filter(item=>item.status==='active')
        let arr=res.data.filter(item=>item.status==='completed')
        let totalValue=arr.reduce((total,item)=>{
          return total+=item.value
        },0)
        let totalCoin=arr.reduce((total,item)=>{
          return total+=item.coin
        },0)

        yield put(creator('saveToDoList', {toDoList,totalValue,totalCoin}));
      }
      return res;
    },
    //获取成就列表
    *getAchievementList({ payload }, { call, put }) {
      const { type } = payload;
      const res = yield call(getToDoList, payload);
     
      if (res.httpStatus === 200) {
        const data = res.data;
        const pieData=pieDataHandle(data)
        if (type === 'all' || type === 'year') {
          //数据处理
          console.log(pieDataHandle(data))
          let value = 0;
          let valueArr = [];
          let coin = 0;
          let coinArr = [];
          let xData = [
            '一月',
            '二月',
            '三月',
            '四月',
            '五月',
            '六月',
            '七月',
            '八月',
            '九月',
            '十月',
            '十一月',
            '十二月',
          ];
          let serName1 = '成就值';
          let serName2 = '成就币';

          for (let i = 0; i < 12; i++) {
            //每个月的起始
            let start = moment()
              .month(i)
              .startOf('month');
            //每个月的结束
            let end = moment()
              .month(i)
              .endOf('month');
            value = 0; //每次循环前清空
            coin = 0;
            data.forEach(item => {
              if (moment(item.date).isBetween(start, end)) {
                value += item.value;
                coin += item.coin;
              }
            });
            valueArr.push(value);
            coinArr.push(coin);
          }

          yield put(
            creator('saveAchievementList', { data, serName1, serName2, xData, valueArr, coinArr,pieData }),
          );
        } else if (type === 'week') {
          let value = 0;
          let valueArr = [];
          let coin = 0;
          let coinArr = [];
          let xData = ['周一', '周二', '周三', '周四', '周五', '周六', '周日'];
          let serName1 = '成就值';
          let serName2 = '成就币';

          for (let i = 0; i < 7; i++) {
            //每周的的起始
            let start = moment()
              .weekday(i)
              .startOf('day');
            //每周的结束
            let end = moment()
              .weekday(i)
              .endOf('day');
            value = 0; //每次循环前清空
            coin = 0;
            data.forEach(item => {
              if (moment(item.date).isBetween(start, end)) {
                value += item.value;
                coin += item.coin;
              }
            });
            valueArr.push(value);
            coinArr.push(coin);
          }

          yield put(
            creator('saveAchievementList', { data, serName1, serName2, xData, valueArr, coinArr,pieData }),
          );
        }else if(type === 'month'){
          let value = 0;
          let valueArr = [];
          let coin = 0;
          let coinArr = [];
          let xData = [];
          let serName1 = '成就值';
          let serName2 = '成就币';
          let total=moment().daysInMonth()
          for (let i = 1; i < total+1; i++) {
            //每月的的起始
            let start = moment()
              .date(i)
              .startOf('day');
            //每月的结束
            let end = moment()
              .date(i)
              .endOf('day');
            let evevyday=moment(start).format('MM-DD')
            xData.push(evevyday);
            value = 0; //每次循环前清空
            coin = 0;
            data.forEach(item => {
              if (moment(item.date).isBetween(start, end)) {
                value += item.value;
                coin += item.coin;
              }
            });
            valueArr.push(value);
            coinArr.push(coin);
          }

          yield put(
            creator('saveAchievementList', { data, serName1, serName2, xData, valueArr, coinArr,pieData }),
          );
        }
      }
      return res;
    },
    *addToDoList({ payload }, { call, put }) {
      const res = yield call(addToDoList, payload);
      return res;
    },
    *operate({ payload }, { call, put }) {
      const res = yield call(operate, payload);
      return res;
    },
    *updateType({ payload }, { call, put }) {
      const res = yield call(updateType, payload);
      return res;
    },
  },
  reducers: {
    saveToDoList(state, { payload }) {
      const {toDoList,totalValue,totalCoin}=payload
      return {
        ...state,
        toDoList,
        totalValue,
        totalCoin
      };
    },
    saveAchievementList(state, { payload }) {
      const { data, serName1, serName2, xData, valueArr, coinArr,pieData } = payload;
      return {
        ...state,
        achievementList: data,
        serName1,
        serName2,
        xData,
        valueArr,
        coinArr,
        pieData
      };
    },
  },
};
